package com.itsu.admin.mvc;

import com.itsu.admin.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Jerry.Su
 * @Date 2021/12/31 16:07
 */
@Slf4j
public class AdminLoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String username = (String) request.getSession().getAttribute(Constant.LOGIN_USER);
        if (!StringUtils.hasText(username)) {
            log.debug("当前session：{}尚未登录..", request.getSession().getId());
            response.sendRedirect("/login?sessionTimeout=true&JSessionId=" + request.getSession().getId());
            return false;
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
